package com.example.flowermall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.flowermall.entity.Product;
import com.example.flowermall.entity.ShoppingCart;
import com.example.flowermall.mapper.ProductMapper;
import com.example.flowermall.mapper.ShoppingCartMapper;
import com.example.flowermall.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiaoman
 * @since 2023-07-10
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    ProductMapper productMapper;
    @Override
    public List<ShoppingCart> select(String userId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        return shoppingCarts;
    }

    @Override
    public void add(ShoppingCart shoppingCart) {
        shoppingCart.setCartTime(LocalDateTime.now());
        Product product = productMapper.selectById(shoppingCart.getProductId());
        shoppingCart.setProductPrice(product.getPrice());
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        if(shoppingCarts!=null){
            for (ShoppingCart cart : shoppingCarts) {
                if (Objects.equals(shoppingCart.getProductId(), cart.getProductId())) {
                    shoppingCartMapper.deleteById(cart.getCartId());
                    shoppingCart.setCartNum(shoppingCart.getCartNum() + cart.getCartNum());
                    shoppingCartMapper.insert(shoppingCart);
                    return ;
                }
            }
        }
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public ShoppingCart getShoppingCartById(Integer id) {
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(id);
        return shoppingCart;
    }

    @Override
    public void deleteCart(Integer id) {
        shoppingCartMapper.deleteById(id);
    }

    @Override
    public void updateCart(ShoppingCart shoppingCart) {
        shoppingCartMapper.updateById(shoppingCart);
    }

    @Override
    public List<ShoppingCart> getCartByUserId(Integer userId) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,userId);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectList(queryWrapper);
        return shoppingCarts;
    }


}
